package com.webbuilder.task;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Date;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.webbuilder.utils.DateUtil;
import com.webbuilder.utils.DbUtil;

public class ClearInvalid implements Job {
	public void execute(JobExecutionContext context)
			throws JobExecutionException {
		System.out.println("开始进行定时删除失效代理code");
		Connection conn = null;
		PreparedStatement delete_st = null;
		PreparedStatement selDays_st = null;
		ResultSet selDays_rs = null;
			try {
				conn = DbUtil.getConnection();
				//更新购买总表
				String deleteSql="delete from trade_market_code where id= ? ";
				delete_st=conn.prepareStatement(deleteSql);
				String dateStr = DateUtil.getDateString(new Date());
				String daysSql="select * from trade_market_code where disabled_date<='"+dateStr+"'";
				selDays_st=conn.prepareStatement(daysSql);
				selDays_rs=selDays_st.executeQuery();
				while(selDays_rs.next()){
					delete_st.setString(1, selDays_rs.getString("id"));
					delete_st.addBatch();
				}
				delete_st.executeBatch();
			} catch (Throwable e) {
				DbUtil.closeConnection(conn, true);
			} finally {
				DbUtil.closeStatement(delete_st);
				DbUtil.closeStatement(selDays_st);
				DbUtil.closeResultSet(selDays_rs);
				DbUtil.closeConnection(conn, false);
			}
	}
}